package com.erp.module.jiaFang.addJiaFang

import android.app.Activity
import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.EditorInfo
import android.widget.ArrayAdapter
import com.erp.activity.BaseActivity
import com.erp.myAdapter.JiaFangAddAdapter
import com.erp.bean.AddJiaFangBean
import com.erp.bean.JiaFangDBean
import com.erp.mclass.UnCeHandler
import com.erp.R
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_addjiafang.*
import kotlinx.android.synthetic.main.header_knoya.*
import java.util.*

/**
 * Created by Administrator on 2018/2/5/005.
 * 作者：刘洋
 */

class AddJiaFangKActivity : BaseActivity(), View.OnClickListener, AddJiaFangKView {

    private val a = "AddJiaFangKActivity"
    private var code = 0
    lateinit var adapter: JiaFangAddAdapter
    lateinit var mlist: MutableList<AddJiaFangBean>
    lateinit var updatedialog: AlertDialog
    lateinit var KP: AddJiaFangKP

    override fun getLayoutId(): Int {
        activityname=a
        return R.layout.activity_addjiafang
    }

    override fun initCreate() {
        UnCeHandler.buzhou = "initCreate"
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
        iv_header_left.visibility = View.VISIBLE
        iv_header_left.setOnClickListener(this)
        code = intent.getIntExtra("code", 0)
        if (code == 1) {
            tv_header_title.text = "添加入仓"
            LlChuCangDanju.visibility = View.GONE
        } else if (code == 2) {
            tv_header_title.text = "添加出仓"
            LlChuCangDanju.visibility = View.VISIBLE
        }
        mlist = ArrayList()
        adapter = JiaFangAddAdapter(this, mlist)
        LvJiaFangAdd.adapter = adapter
        KP= IAddJiaFangKP(this, this, mlist, code)
        IvSaoMiaoBianMa.setOnClickListener(this)
        IvSaoMiaoDanJu.setOnClickListener(this)
        BtQueding.setOnClickListener(this)
        BtQuedingDanJu.setOnClickListener(this)
        BtQuedingBianMa.setOnClickListener(this)
        EtShuRuDanJu.setOnEditorActionListener { _, actionId, event ->
            var isOK = true
            when (actionId) {
                EditorInfo.IME_ACTION_DONE -> {
                    updatedialog = Myutil.showupdatedialog(this@AddJiaFangKActivity, "正在刷新", false)
                    KP.getDanJu(EtShuRuDanJu.text.toString())
                    isOK = false
                }
                else -> isOK = false
            }
            isOK
        }
        EtShuRuBianMa.setOnEditorActionListener { v, actionId, event ->
            var isOK = true
            when (actionId) {
                EditorInfo.IME_ACTION_DONE -> {
                    updatedialog = Myutil.showupdatedialog(this@AddJiaFangKActivity, "正在刷新", false)
                    KP.getBianMa(EtShuRuBianMa.text.toString())
                }
                else -> isOK = false
            }
            isOK
        }
        val listsp = arrayOf("专卖店用品仓","中间仓","五金仓","其它材料仓","华标三区仓","原料仓","饰品仓")
        val adapter = ArrayAdapter(this@AddJiaFangKActivity, R.layout.lv_sp_tvitem, listsp)
        SpCangKu.adapter = adapter
        EtShuRuBianMa.requestFocus()
    }

    override fun onClick(v: View) {
        UnCeHandler.buzhou = "onClick"
        when (v) {
            iv_header_left -> fin()
            IvSaoMiaoBianMa -> {
                Myutil.saoma(this@AddJiaFangKActivity,null,1)
            }
            IvSaoMiaoDanJu -> {
                Myutil.saoma(this@AddJiaFangKActivity,null,2)
            }
            BtQueding -> {
                updatedialog = Myutil.showupdatedialog(this@AddJiaFangKActivity, "正在上传", false)
                KP.addJiaFang(SpCangKu.selectedItem.toString(),EtBeiZhu.text.toString())
            }
            BtQuedingDanJu -> {
                updatedialog = Myutil.showupdatedialog(this@AddJiaFangKActivity, "正在刷新", false)
                KP.getDanJu(EtShuRuDanJu.text.toString())
            }
            BtQuedingBianMa -> {
                updatedialog = Myutil.showupdatedialog(this@AddJiaFangKActivity, "正在刷新", false)
                KP.getBianMa(EtShuRuBianMa.text.toString())
            }
        }
    }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        UnCeHandler.buzhou = "onActivityResult"
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        if(resultCode==Activity.RESULT_OK){
            when(requestCode){
                1 -> {
                    val bundle = data.extras
                    val s = bundle?.get("result") as String
                    EtShuRuBianMa.setText(s)
                    updatedialog = Myutil.showupdatedialog(this@AddJiaFangKActivity, "正在刷新", false)
                    KP.getBianMa(EtShuRuBianMa.text.toString())
                }
                2 -> {
                    val bundle = data.extras
                    val s = bundle?.get("result") as String
                    EtShuRuDanJu.setText(s)
                    updatedialog = Myutil.showupdatedialog(this@AddJiaFangKActivity, "正在刷新", false)
                    KP.getDanJu(EtShuRuDanJu.text.toString())
                }
            }
        }
    }

    override fun getBianMaOk(bean: AddJiaFangBean) {
        UnCeHandler.buzhou = "getBianMaOk"
        updatedialog.dismiss()
        TvBianMaTiShi.visibility = View.GONE
        mlist.add(bean)
        adapter.notifyDataSetChanged()
    }

    override fun getBianMaNo(e: String) {
        UnCeHandler.buzhou = "getBianMaNo"
        updatedialog.dismiss()
        if (e != ""){
            TvBianMaTiShi.visibility = View.VISIBLE
            TvBianMaTiShi.text = e
        }
    }
    override fun getDanHaoOK(danjuBean : JiaFangDBean) {
        UnCeHandler.buzhou = "getDanHaoOK"
        updatedialog.dismiss()
        LlDanJu.visibility = View.VISIBLE
        TvDanJuTiShi.visibility = View.GONE
        TvShengChanDan.text = danjuBean.o_onum
        TvDanJuLeiXing.text = danjuBean.ot_name
        TvDanJuDian.text = danjuBean.shop_name
    }

    override fun getDanHaoNO(s : String) {
        UnCeHandler.buzhou = "getDanHaoNO"
        updatedialog.dismiss()
        LlDanJu.visibility = View.GONE
        TvDanJuTiShi.visibility = View.VISIBLE
        TvDanJuTiShi.text=s
    }

    override fun addJiaFangOk() {
        UnCeHandler.buzhou = "addJiaFangOk"
        updatedialog.dismiss()
        val intent = Intent()
        setResult(Activity.RESULT_OK, intent)
        finish()
    }

    override fun addJiaFangNo() {
        updatedialog.dismiss()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if(keyCode==4){
            fin()
        }
        return super.onKeyDown(keyCode, event)
    }
    fun fin(){
        UnCeHandler.buzhou = "fin"
        val dialog = AlertDialog.Builder(this@AddJiaFangKActivity)
        dialog.setTitle("提示")
        dialog.setMessage("是否取消新建？")
        dialog.setPositiveButton("确定") { dialog, which ->
            finish()
            dialog.dismiss()
        }
        dialog.setNegativeButton("取消") { dialog, _ ->
            dialog.dismiss()
        }
        dialog.show()
    }
    //    SELECT*FROM `k_inventory_iogoods` ORDER BY `k_inventory_iogoods`.`id` DESC  LIMIT 0,1 \G;
}
